package admin

import (
	"config-client-v1/model"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type HomeController struct {
}

func (con HomeController) DictList(ctx *gin.Context) {
	param := struct {
		Type string `json:"type"`
	}{}
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}
	tx := model.DB.Model(&model.Dict{})
	if param.Type != "" {
		tx.Where("type = ?", param.Type)
	}
	list := make([]*model.Dict, 0)
	err = tx.Order("id ASC").Find(&list).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data": map[string]interface{}{
			"list": list,
		},
	})
}
